local skel = fk.CreateSkill {
  name = "emo__huansi",
}

Fk:loadTranslationTable{
  ["emo__huansi"] = "欢思",
  [":emo__huansi"] = "准备阶段，若场上没有“欢思”标记，你可以将四种花色“欢思”分配给任意角色。当一名有此标记的角色使用对应花色的牌或受到此花色牌的伤害后，其可以转移此标记，并与对方各摸一张牌。",

  ["#emo__huansi-invoke"] = "你可以将四种花色“欢思”分配给任意角色！",
  ["#emo__huansi-choose"] = "请将【%arg】“欢思”标记交给一名角色，其使用此花色牌或受此花色牌伤害后可摸牌",
  ["@emo__huansi"] = "欢思",
  ["#emo__huansi-transfer"] = "欢思：你可以将【%arg】“欢思”标记交给另一名角色，与其各摸一张牌",
}

skel:addEffect(fk.EventPhaseStart, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(skel.name) and target == player and player.phase == Player.Start then
      return table.every(player.room.alive_players, function (p)
        return p:getMark("@emo__huansi") == 0
      end)
    end
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {skill_name = skel.name, prompt = "#emo__huansi-invoke" })
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local suits = {"log_spade", "log_heart", "log_diamond", "log_club"}
    for _, suit in ipairs(suits) do
      local to = room:askToChoosePlayers(player, {
        targets = room.alive_players, min_num = 1, max_num = 1, skill_name = skel.name, cancelable = false,
        prompt = "#emo__huansi-choose:::"..suit
      })[1]
      room:addTableMarkIfNeed(to, "@emo__huansi", suit)
    end
  end,
})

local can_trigger = function (self, event, target, player, data)
  return target == player and not player.dead and data.card and
  table.contains(player:getTableMark("@emo__huansi"), data.card:getSuitString(true))
end

local on_cost = function (self, event, target, player, data)
  local tos = player.room:askToChoosePlayers(player, {
    targets = player.room:getOtherPlayers(player, false), min_num = 1, max_num = 1, skill_name = skel.name,
    prompt = "#emo__huansi-transfer:::"..data.card:getSuitString(true),
  })
  if #tos > 0 then
    event:setCostData(self, {tos = tos})
    return true
  end
end

local on_use = function (self, event, target, player, data)
  local room = player.room
  local suit = data.card:getSuitString(true)
  room:removeTableMark(player, "@emo__huansi", suit)
  local to = event:getCostData(self).tos[1]
  room:addTableMarkIfNeed(to, "@emo__huansi", suit)
  if player:isAlive() then
    player:drawCards(1, skel.name)
  end
  if to:isAlive() then
    to:drawCards(1, skel.name)
  end
end

skel:addEffect(fk.Damaged, {
  is_delay_effect = true,
  anim_type = "drawcard",
  can_trigger = can_trigger,
  on_cost = on_cost,
  on_use = on_use,
})

skel:addEffect(fk.CardUseFinished, {
  is_delay_effect = true,
  anim_type = "drawcard",
  can_trigger = can_trigger,
  on_cost = on_cost,
  on_use = on_use,
})

return skel
